package com.ygxsk.carhome.ui.adapter;

import android.text.TextUtils;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.enumwrap.PropertyMentType;
import com.ygxsk.carhome.response.PropertyPaymentBean;
import com.ygxsk.carhome.utils.CommUtils;

import java.math.BigDecimal;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-12-20  13:56 2018/1/25 0025.第一版本的开发页面
 * @Description: 物业缴费的适配器内容
 */
public class PropertyPaymentAdapter extends BaseQuickAdapter<PropertyPaymentBean, BaseViewHolder> {

    /**
     * 从哪个界面跳转过来的.1是从缴费列表过来的，2是从费用支付和缴费详情进来的，不需要勾选功能
     */
    private int flag_jumpfrom = 0;

//    public PropertyPaymentAdapter(int layoutResId, @Nullable List<PropertyPaymentBean> data) {
//        super(layoutResId, data);
//    }

    public PropertyPaymentAdapter(int layoutResId, @Nullable List<PropertyPaymentBean> data, int _flag) {
        super(layoutResId, data);

        flag_jumpfrom = _flag;
        //mSession = session;

        ////2018年08月09日去掉全选功能
        //默认情况下都是去选中的状态

    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, PropertyPaymentBean propertyPaymentBeanItem) {

        if (propertyPaymentBeanItem != null) {

            //2018年08月18日新增小区名称【XXXXXX】
            String roomNumber = propertyPaymentBeanItem.getHouseNo();
            String communityName = propertyPaymentBeanItem.getCommunityName();//"communityName": "测试小区西区",

            StringBuffer stringBufferCommunity = new StringBuffer();

            //1-1-201 【盛邦花园六区】
            stringBufferCommunity.append(roomNumber);
            stringBufferCommunity.append(getContext().getResources().getString(R.string.kongge0));

            //stringBufferCommunity.append("【");
            stringBufferCommunity.append("(");
            //stringBufferCommunity.append(mContext.getResources().getString(R.string.kongge));
            stringBufferCommunity.append(communityName);
            //stringBufferCommunity.append(mContext.getResources().getString(R.string.kongge));
            //stringBufferCommunity.append("】");
            stringBufferCommunity.append(")");

            String paymentName = propertyPaymentBeanItem.getPaymentName();
            String paymentRemarks = propertyPaymentBeanItem.getRemark();

            //需要对两个时间做判断
            String paymentStartTime = propertyPaymentBeanItem.getStartTime();
            String paymentEndTime = propertyPaymentBeanItem.getEndTime();

            StringBuffer stringBuffer = new StringBuffer();

            if (!TextUtils.isEmpty(paymentStartTime)) {
                stringBuffer.append(paymentStartTime);
                stringBuffer.append(" 至 ");
            } else {
                //LogUtils.d("itchen---paymentStartTime = null");
            }

            if (!TextUtils.isEmpty(paymentEndTime)) {
                stringBuffer.append(paymentEndTime);
            } else {
                //LogUtils.d("itchen---paymentEndTime = null");
            }

            //最终的截止日期是:
            String paymentDateInterval = stringBuffer.toString();
            BigDecimal paymentAmount = propertyPaymentBeanItem.getPayAmount();
            BigDecimal paymentAmountfromat;

            if (paymentAmount != null) {
                paymentAmountfromat = CommUtils.formatComma2BigDecimal(paymentAmount);
            } else {
                paymentAmountfromat = CommUtils.formatComma2BigDecimal(CommUtils.valueOf(0.00));
            }

//            RelativeLayout layoutCheckBox = (RelativeLayout) baseViewHolder.getView(R.id.layoutcheckbox);

            //勾选的复选框
            CheckBox checkBoxInLiveAdapterProperty = (CheckBox) baseViewHolder.getView(R.id.checkBoxInLiveAdapterProperty);

//            public enum PropertyMentType {
//                TYPE_TO_BE_PAY("1", "待支付"),
//                TYPE_GO_TO_PAY("2", "去结算");

            //判断是从缴费列表过来的还是从缴费的支付过来的。
            if (flag_jumpfrom == PropertyMentType.TYPE_TO_BE_PAY.getIndex()) {//从缴费列表过来的,待支付

                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "PropertyMentType.TYPE_TO_BE_PAY");
                CommUtils.setViewVisible(checkBoxInLiveAdapterProperty);

                int isChecked = propertyPaymentBeanItem.getChecked();
//                CheckBox checkBoxSeletcItem = (CheckBox) baseViewHolder.getView(R.id.imagepaymentselect);
//                CommUtils.setButtonDrawable(checkBoxSeletcItem);

                if (isChecked == 0) {
                    checkBoxInLiveAdapterProperty.setChecked(false);
                } else if (isChecked == 1) {
                    checkBoxInLiveAdapterProperty.setChecked(true);
                }

            } else if (flag_jumpfrom == PropertyMentType.TYPE_GO_TO_PAY.getIndex()) {//从费用支付和缴费详情过来，去结算的结算页

                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "从费用支付和缴费详情过来--PropertyMentType.TYPE_GO_TO_PAY");
                CommUtils.setViewGone(checkBoxInLiveAdapterProperty);

            }

            //helper.setText(R.id.textpayroomnuinlist, TextUtils.isEmpty(roomNumber) ? "" : roomNumber);
            baseViewHolder.setText(R.id.textpayroomnuinlist, TextUtils.isEmpty(roomNumber) ? "" : stringBufferCommunity.toString());

            baseViewHolder.setText(R.id.textViewpaymentname, TextUtils.isEmpty(paymentName) ? "" : paymentName);
            baseViewHolder.setText(R.id.textvieworder, TextUtils.isEmpty(paymentRemarks) ? "" : paymentRemarks);
            baseViewHolder.setText(R.id.textpaydatetime, TextUtils.isEmpty(paymentDateInterval) ? "" : paymentDateInterval);

            baseViewHolder.setText(R.id.textpaytypedes, MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + paymentAmountfromat);

            //2018年03月21日 因为为了扩大点击区域使用onItemClick
            //helper.addOnClickListener(R.id.layoutcheckbox);//绑定一个点击事件内容
            //转换成checkbox来处理内容


        }

    }

    /**
     * 全部选中的状态
     */
    public void selectedAll() {

        if (!this.getData().isEmpty()) {//如果列表不是空的

            for (int index = 0; index < getData().size(); index++) {

                //PropertyPaymentBean propertyPaymentBean = getData().get(index);
                //propertyPaymentBean.setChecked(1);//全部标志选中状态
                getData().get(index).setChecked(1);

            }
        }

        notifyDataSetChanged();

    }

    /**
     * 标志全部未选中
     */
    public void unSelectedAll() {

        if (!this.getData().isEmpty()) {
            for (int index = 0; index < getData().size(); index++) {
                getData().get(index).setChecked(0);
            }
        }
        notifyDataSetChanged();

    }

}
